<!-- pages/login/components/mobile.vue -->
<script setup>
	import { ref } from 'vue';
	import { getMobileCMSAPI, loginByCMSAPI } from '../../../apis/login';
	import { useUserStore } from '@/stores'
	// 是否显示倒计时组件
	const showCountdown = ref(false)
	
	const buttonText = ref('获取验证码')
	const form = ref(null)
	
	// 准备响应式数据
	const formData = ref({
		mobile: '',
		code: ''
	})
	
	// 我同意按钮
	const onChecked = ref(false)
	
	// 定义表单校验的规则
	const formRules = {
		mobile: {
			rules: [
				{
					required: true,
					errorMessage: '请输入手机号码',
				},
				{ pattern: /^(?:(?:\+|00)86)?1(?:(?:3[\d])|(?:4[5-79])|(?:5[0-35-9])|(?:6[5-7])|(?:7[0-8])|(?:8[\d])|(?:9[1589]))\d{8}$/,
				 errorMessage: '手机号格式不正确' }
			]
		},
		code: {
			rules: [
				{
					required: true,
					errorMessage: '请输入验证码',
				},
				{ pattern: /^\d{6}$/, errorMessage: '验证码格式不正确' }
			]
		}
	}
	
	// 点击获取验证码
	const onCountdown = async () => {
		// 点击获取验证码时验证手机号是否输入
		await form.value.validateField(['mobile'])
		// 发送请求
		const res = await getMobileCMSAPI({
			mobile: formData.value.mobile,
			type: 'login'
		})
		console.log(res, 222222)
		// 显示倒计时组件
		showCountdown.value = true
	}
	
	// 倒计时结束触发
	const onTimeup = () => {
		// 关闭倒计时组件
		showCountdown.value = false
		// 更改按钮文字
		buttonText.value = '重新获取验证码'
	}
	
	// 点击我同意按钮
	const radioChange = () => {
		onChecked.value = !onChecked.value
	}
	
	// 点击登录按钮
	const onLogin = async () => {
		if(onChecked.value === false) {
			return uni.showToast({
				icon:'none',
				title:'请勾选我同意协议'
			})
		}
		try{
			// 进行表单校验
			await form.value.validate()
			// 发请求
			const { data } = await loginByCMSAPI(formData.value)
			// console.log(res, 848484)
			// 将数据存储到pinia中
			const store = useUserStore()
			// 调用useUserStore中的方法
			store.setUser(data)
			uni.showToast({
				icon: 'success',
				title: '登录成功'
			})
			// 登陆成功后跳转页面
			// console.log(store.openType, 909090)
			uni[store.openType]({
				url: store.redirectURL
			})
			
		}catch(e){
			//TODO handle the exception
			console.log(e)
		}
	}
	
</script>

<template>
  <uni-forms class="login-form" ref="form" :model="formData" :rules="formRules">
    <uni-forms-item name="mobile">
      <uni-easyinput
        :input-border="false"
        :clearable="false"
        placeholder="请输入手机号"
        placeholder-style="color: #C3C3C5"
		v-model="formData.mobile"
      />
    </uni-forms-item>
    <uni-forms-item name="code">
      <uni-easyinput
        :input-border="false"
        :clearable="false"
        placeholder="请输入验证码"
        placeholder-style="color: #C3C3C5"
		v-model="formData.code"
      />
	  <view class="text-button" v-if="showCountdown === true">
		<uni-countdown :second="59"
			:show-day="false"
			:show-hour="false"
			:show-minute="false"
			color="#16c2A3"
			@timeup="onTimeup"
			>
		</uni-countdown>秒发送验证码
	  </view>
      <text v-else class="text-button" @click="onCountdown">{{ buttonText }}</text>
    </uni-forms-item>

    <view class="agreement">
      <radio :checked="onChecked" color="#16C2A3" @click="radioChange"/>
      我已同意
      <text class="link">用户协议</text>
      及
      <text class="link">隐私协议</text>
    </view>

    <button class="uni-button" @click="onLogin">登 录</button>
  </uni-forms>
</template>

<script>
export default {
  options: {
    styleIsolation: 'shared'
  }
}
</script>

<style lang="scss">
@import './styles.scss';
</style>